package zct.proj.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import zct.proj.R;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter{
		 
    private LayoutInflater mInflater;
    private List<HashMap<String, String>> mData;
    private ListItemInterface ctx;
    private List<String> mAddedList;
     
    public CustomAdapter(ListItemInterface context, List<HashMap<String, String>> data){
    	ctx = context;
        this.mInflater = LayoutInflater.from((Context) ctx);
        this.mData = data;
    }
    
    @Override
    public int getCount() {
        return mData.size();
    }
 
    @Override
    public Object getItem(int arg0) {
        return null;
    }
 
    @Override
    public long getItemId(int arg0) {
        return 0;
    }
    
    public void clear() {
    	mData.clear();
    }
    
    public void setAddedList(List<String> addedList) {
    	mAddedList = addedList;
    }
    
    public void setData(List<HashMap<String, String>> data) {
    	mData = data;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
             
        ViewHolder holder = null;
        if (convertView == null) {
            holder=new ViewHolder();  
            convertView = mInflater.inflate(R.layout.listitem, null);
            holder.title = (TextView)convertView.findViewById(R.id.title);
            holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
            convertView.setTag(holder);
            convertView.setBackgroundColor(Color.BLACK);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        // 读取当前行对应的开关名
        HashMap<String, String> lineMap = mData.get(position);
        String t = lineMap.get("title");
        holder.title.setText(t);
        // 根据开关名改变开关文字颜色
		if (t.endsWith("H")) {
			holder.title.setTextColor(Color.RED);
		} else {
			holder.title.setTextColor(Color.WHITE);
		}
        // 在已添加的开关列表中查找当前开关名是否已存在
//        String addedSwName = null;
        
//        for (HashMap<String, String> line : mData) {
//        	if (mAddedList.contains(line.get("title"))) {
//        		// 当前开关已被添加
//        		addedSwName = line.get("title");
//        		//break;
//        	}
//        }
        
		// 开关是否已添加
		boolean swAdded = mAddedList.contains(t);
//    	if (mAddedList.contains(t)) {
//    		// 当前开关已被添加
//    		addedSwName = line.get("title");
//    		//break;
//    	}
		// 当前开关的配对开关已被添加
    	boolean foundPair = false;
    	if (!swAdded) {
			t = t.substring(0, t.length() - 1);
			for (String s : mAddedList) {
				if (s.substring(0, s.length() - 1).equals(t)) {
					foundPair = true;
					break;
				}
			}
    	}
        
		String btnText = null;
		if (swAdded) {
			btnText = "取消";
		} else {
			if (!foundPair) {
				// 没有配对
				btnText = "添加";
			}
		}
        if (btnText != null) {
	    	holder.viewBtn.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                ctx.itemButtonClicked(v);
	            }
	        });
	    	holder.viewBtn.setText(btnText);
	    	holder.viewBtn.setTag(lineMap.get("title"));
	    	holder.viewBtn.setVisibility(View.VISIBLE);
        } else {
        	holder.viewBtn.setVisibility(View.INVISIBLE);
        }
        
        return convertView;
    }
}